package com.example.recyclerview1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private Context mContext;

    RecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, parent, false));
    }

    // 绑定ViewHolder
    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
        holder.textView.setText("hello" + position);//划到底端再划上去，上面的item会执行这个
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "你点击了第" + position + "个按钮", Toast.LENGTH_SHORT).show();
                holder.textView.setText("你已点击过" + position);
            }
        });
    }

    // 数量
    @Override
    public int getItemCount() {
        return 30;
    }

    // 定义第一个ViewHolder
    class ViewHolder extends RecyclerView.ViewHolder  {

        private TextView textView;
        private Button btn;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.rv_tv);
            btn = itemView.findViewById(R.id.rv_btn);
        }
    }
}
